package com.qingcheng.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.qingcheng.dao.SeckillGoodsMapper;
import com.qingcheng.dao.SeckillOrderMapper;
import com.qingcheng.pojo.seckill.SeckillGood;
import com.qingcheng.pojo.seckill.SeckillOrder;
import com.qingcheng.pojo.seckill.SeckillStatus;
import com.qingcheng.service.seckill.SeckillGoodsService;
import com.qingcheng.service.seckill.SeckillOrderService;
import com.qingcheng.task.MultiThreadingCreateOrder;
import com.qingcheng.utils.CacheKey;
import com.qingcheng.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.Date;

/**
 * Created by yuanxin
 * Date:2019/7/23
 * Time:19:19
 */
@Service
public class SeckillOrderServiceImpl implements SeckillOrderService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private MultiThreadingCreateOrder multiThreadingCreateOrder;
    @Autowired
    private SeckillOrderMapper seckillOrderMapper;

    /**
     * 添加订单
     *
     * @param id
     * @param time
     * @param username
     * @return
     */
    @Override
    public Boolean add(Long id, String time, String username) {
        //递增，判断是否排队
        Long increment = redisTemplate.boundHashOps(CacheKey.USERQUEUECOUNT).increment(username, 1);
        if (increment > 1) {
            //100  标识重复抢单
            System.out.println("重复抢单");
            throw new RuntimeException("100");
        }
        Long size = redisTemplate.boundListOps(CacheKey.SECKILLGOODSCOUNTLIST_.toString() + id).size();
        if (size == null || size <= 0) {
            //没有库存
            throw new RuntimeException("101");
        }

        //创建队列所需数据
        SeckillStatus seckillStatus = new SeckillStatus(username, new Date(), 1, id, time);
        redisTemplate.boundListOps(CacheKey.SECKILLORDERQUEUE).leftPush(seckillStatus);
        //查询信息
        redisTemplate.boundHashOps(CacheKey.USERQUEUESTATUS).put(username, seckillStatus);
        multiThreadingCreateOrder.createOrder();
        return true;
    }

    @Override
    public SeckillStatus queryStatus(String username) {
        return (SeckillStatus) redisTemplate.boundHashOps(CacheKey.USERQUEUESTATUS).get(username);
    }

    @Override
    public void updatePayStatus(String outTradeNo, String transactionId, String username) {
        //订单数据从Redis数据库查询出来
        SeckillOrder seckillOrder = (SeckillOrder) redisTemplate.boundHashOps("SeckillOrder").get(username);
        //修改状态
        seckillOrder.setStatus("1");
        seckillOrder.setPayTime(new Date());
        seckillOrderMapper.insertSelective(seckillOrder);

        redisTemplate.boundHashOps("SeckillOrder").delete(username);
        redisTemplate.boundHashOps(CacheKey.USERQUEUECOUNT).delete(username);
        redisTemplate.boundHashOps(CacheKey.USERQUEUESTATUS).delete(username);
    }

    @Override
    public SeckillOrder findById(String username) {
        return (SeckillOrder) redisTemplate.boundHashOps("SeckillOrder").get(username);
    }
}
